package com.nikitova.craps3d.controller {
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	import org.puremvc.as3.multicore.utilities.statemachine.FSMInjector;

	import com.nikitova.craps3d.constants.statemachine.StateActions;
	import com.nikitova.craps3d.constants.statemachine.StateNames;
	import com.nikitova.craps3d.constants.statemachine.StateTransitions;

	public class InjectFSMCommand extends SimpleCommand
	{
		/**
		 * Inject the Finite State Machine.
		 * <P>
		 * Though the XML could come from anywhere, even loaded remotely,
		 * we are simply creating it here in this command since it is 
		 * easy to refefence the StopWatch ACTION and STATE constants,
		 * so they will match up with the events acutally being sent from
		 * the stopwatch view.</P>
		 * 
		 * <P>
		 * Then we use the <code>FSMInjector</code> to create the configured 
		 * <code>StateMachine</code> from the XML FSM description and 'inject' 
		 * it into the PureMVC apparatus.</P>
		 *
		 * <P> 
		 * The <code>StateMachine</code> is registered as an <code>IMediator</code>, 
		 * interested in <code>StateMachine.ACTION</code> Notifications. The 
		 * <code>type</code> parameter of those Notifications must be a valid 
		 * registered <code>State</code> in the FSM.</P>
		 */
		override public function execute ( note:INotification ) : void
		{
			// Create the FSM definition
			var fsm:XML = 
			<fsm initial={StateNames.PRELOADER}>
			   <state name={StateNames.PRELOADER} entering={StateTransitions.SHOW_PRELOADER} exiting={StateTransitions.HIDE_PRELOADER}>
			       <transition action={StateActions.GO_BET_STATE}			target={StateNames.BET_STATE}/>
			   </state>
			   <state name={StateNames.BET_STATE} entering={StateTransitions.SHOW_BET_STATE} exiting={StateTransitions.HIDE_BET_STATE}>
			       <transition action={StateActions.GO_ROLL_STATE}			target={StateNames.ROLL_STATE}/>
			   </state>
			   <state name={StateNames.ROLL_STATE} entering={StateTransitions.SHOW_ROLL_STATE} exiting={StateTransitions.HIDE_ROLL_STATE}>
			       <transition action={StateActions.GO_BET_STATE}			target={StateNames.BET_STATE}/>
			   </state>
			</fsm>;
			
			// Create and inject the StateMachine 
			var injector:FSMInjector = new FSMInjector( fsm );
			injector.initializeNotifier(this.multitonKey);
			injector.inject();
		}
	}
}
